using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public class tbnguoidungDAL
    {
       private SqlConnection cn = null;
        public tbnguoidungDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbnguoidungDAL(tbnguoidungDAL nguoidung)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbnguoidung nguoidung)
        {
            SqlCommand cm = new SqlCommand("nguoidung_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@maloainguoidung", nguoidung.Maloainguoidung);
            cm.Parameters.AddWithValue("@emaildangnhap", nguoidung.Emaildangnhap);
            cm.Parameters.AddWithValue("@matkhau", nguoidung.Matkhau);
            cm.Parameters.AddWithValue("@sodienthoai", nguoidung.Sodienthoai);
            cm.Parameters.AddWithValue("@diachigiaohang", nguoidung.Diachigiaohang);
            cm.Parameters.AddWithValue("@kichhoatdienthoai", nguoidung.Kichhoatdienthoai);
            cm.Parameters.AddWithValue("@tongtientrongvi", nguoidung.Tongtientrongvi);
            cm.Parameters.AddWithValue("@hinhdaidien", nguoidung.Hinhdaidien);
            cm.Parameters.AddWithValue("@gioitinh", nguoidung.Gioitinh);
            cm.ExecuteNonQuery();
        }
        public void Update(tbnguoidung nguoidung)
        {
            SqlCommand cm = new SqlCommand("nguoidung_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@manguoidung", nguoidung.Manguoidung);
            cm.Parameters.AddWithValue("@maloainguoidung", nguoidung.Maloainguoidung);
            cm.Parameters.AddWithValue("@emaildangnhap", nguoidung.Emaildangnhap);
            cm.Parameters.AddWithValue("@matkhau", nguoidung.Matkhau);
            cm.Parameters.AddWithValue("@sodienthoai", nguoidung.Sodienthoai);
            cm.Parameters.AddWithValue("@diachigiaohang", nguoidung.Diachigiaohang);
            cm.Parameters.AddWithValue("@kichhoatdienthoai", nguoidung.Kichhoatdienthoai);
            cm.Parameters.AddWithValue("@tongtientrongvi", nguoidung.Tongtientrongvi);
            cm.Parameters.AddWithValue("@hinhdaidien", nguoidung.Hinhdaidien);
            cm.Parameters.AddWithValue("@gioitinh", nguoidung.Gioitinh);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("nguoidung_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@manguoidung", iD);
            cm.ExecuteNonQuery();
        }
        public tbnguoidungcollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("nguoidung_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbnguoidungcollection nguoidung1Collection = new tbnguoidungcollection();
            while (dr.Read())
            {
                tbnguoidung nguoidung = new tbnguoidung();
                nguoidung.Manguoidung= int.Parse(dr.GetValue(0).ToString());
                nguoidung.Maloainguoidung = int.Parse(dr.GetValue(1).ToString());
                nguoidung.Emaildangnhap = dr.GetValue(2).ToString();
                nguoidung.Matkhau = dr.GetValue(3).ToString();
                nguoidung.Sodienthoai = dr.GetValue(4).ToString();
                nguoidung.Diachigiaohang = dr.GetValue(5).ToString();
                nguoidung.Kichhoatdienthoai = int.Parse(dr.GetValue(6).ToString());
                nguoidung.Tongtientrongvi = int.Parse(dr.GetValue(7).ToString());
                nguoidung.Hinhdaidien = dr.GetValue(8).ToString();
                nguoidung.Gioitinh = bool.Parse(dr.GetValue(9).ToString());
                nguoidung1Collection.Add(nguoidung);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return nguoidung1Collection;
        }
    }
}
